package mybatis;/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved.
 */

import org.opengauss.mapper.ByteaMapper;
import org.opengauss.vo.ByteaVo;
import common.testcase.BaseMyBatisTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
 * description:this for BlobTest Class
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021.
 *
 * @author Administrator
 * @version [openGauss_debug 0.0.1 2021/3/11]
 * @since 2021/3/11
 */
public class ByteaTest extends BaseMyBatisTestCase<ByteaMapper> {
    @Before
    public void setUp() throws IOException {
        initSession();
        mapper.dropTable();
        mapper.createTable();
        initAutoId(mapper);
    }

    @After
    public void tearDown() throws SQLException {
        mapper.dropTable();
        closeSession();
    }

    @Test
    public void queryTest() throws SQLException {
        List<ByteaVo> result = mapper.selectAll();
        assertEquals(0, result.size());
    }

    @Test
    public void insertAndQueryTest() throws SQLException {
        ByteaVo byteaVo = new ByteaVo();
        byteaVo.id = autoId.getAndIncrement();
        byteaVo.descdata = new String("this is simple test:" + byteaVo.id).getBytes(StandardCharsets.UTF_8);
        byteaVo.val1 = new String("this is simple test111:" + byteaVo.id);
        int insert = mapper.insertByteaOne(byteaVo);
        assertEquals(1, insert);

        ByteaVo queryBlob = mapper.selectByteaById(byteaVo.id);
        assertNotNull(queryBlob);
        assertEquals(byteaVo.id, queryBlob.id);
        assertEquals(new String(byteaVo.descdata), new String(queryBlob.descdata));
        assertEquals(byteaVo.val1, queryBlob.val1);
    }

    @Override
    public ByteaMapper getMapper() {
        return getMapper(ByteaMapper.class);
    }

    @Override
    public int getMaxId(ByteaMapper mapper) {
        return mapper.selectMaxId();
    }
}
